package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmsProductCategory;
import com.macro.mall.model.PmsProductEvaluationTags;
import com.macro.mall.model.UmsAdmin;
import com.macro.mall.service.PmsProductCategoryService;
import com.macro.mall.service.PmsProductEvaluationTagsService;
import com.macro.mall.service.UmsAdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author：Minxiao-Hao
 * @Description: 商品标签管理
 * @name：PmsProductEvaluationTags
 * @Date：2024/10/25 17:07
 */
@RestController
@Tag(name = "PmsProductEvaluationTagsController", description = "商品标签管理")
@RequestMapping("/tags")
public class PmsProductEvaluationTagsController {
    @Autowired
    private PmsProductEvaluationTagsService psProductEvaluationTagsService;
    @Autowired
    private UmsAdminService umsAdminService;
    @Autowired
    private PmsProductCategoryService productCategoryService;
    @Operation(summary = "添加标签")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult add(@RequestBody PmsProductEvaluationTags pmsProductEvaluationTags) {
        UmsAdmin currentAdmin = umsAdminService.getCurrentAdmin();
        pmsProductEvaluationTags.setCreateBy(currentAdmin.getUsername());
        pmsProductEvaluationTags.setCreateTime(new Date());
        int count = psProductEvaluationTagsService.add(pmsProductEvaluationTags);

        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @Operation(summary = "删除标签")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResult delete(@PathVariable Long id) {
        int count = psProductEvaluationTagsService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @Operation(summary = "修改标签")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public CommonResult update(@PathVariable Long id, @RequestBody PmsProductEvaluationTags pmsProductEvaluationTags) {
        pmsProductEvaluationTags.setUpdateTime(new Date());
        UmsAdmin currentAdmin = umsAdminService.getCurrentAdmin();
        pmsProductEvaluationTags.setUpdateBy(currentAdmin.getUsername());
        int count = psProductEvaluationTagsService.update(id, pmsProductEvaluationTags);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @Operation(summary = "根据分类id获取旗下的标签")
    @RequestMapping(value = "/list/{categoryId}", method = RequestMethod.GET)
    public CommonResult<List<PmsProductEvaluationTags>> list(@PathVariable Long categoryId){
        List<PmsProductEvaluationTags> list = psProductEvaluationTagsService.selectList(categoryId);
        return CommonResult.success(list);
    }

    @Operation(summary = "获取标签详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<PmsProductEvaluationTags> getItem(@PathVariable Long id) {
        PmsProductEvaluationTags address = psProductEvaluationTagsService.getItem(id);
        return CommonResult.success(address);
    }

}
